//定义懒加载插件

import { useIntersectionObserver } from '@vueuse/core'

export const lazyPlugin = {
    install(app) {
        //懒加载指令逻辑
        app.directive('img-lazy', {
            mounted(el, binding) {
                //el:指令绑定的元素，img
                //binding：指令对象，binding.vlaue  指令等于号后面绑定的值，图片url
                const {stop}= useIntersectionObserver(
                    //el:监听的元素
                    //callback：监听到的回调函数
                    el,
                    ([{ isIntersecting }]) => {
                        console.log(isIntersecting)
                        if (isIntersecting) {
                            //加载图片
                            el.src = binding.value
                            stop()
                        }
                    }
                )
        
            }
        }
        
        )
        
    }
}